<?php
/**
 * RecursiveTrait.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/5/10
 * Time: 14:52
 */
namespace app\common;

trait RecursiveTrait
{
    /**
     * 递归层级关系
     * @param  array $arr 原数组引用
     * @param  integer $pid 终止条件
     * @return array
     */
    public static function recursive(&$arr,$pid=0,$id_name='id',$p_name='pid',$child_name='children'){
        $res = array();
        foreach($arr as $key => $value){
            if($value[$p_name] == $pid){
                $value[$child_name] = self::recursive($arr,$value[$id_name],$id_name,$p_name,$child_name);
                $res[] = $value;
            }
        }
        return $res;
    }
}